package com.cyh.myapplication.weex;

/**
 * @author youhai.cai
 *         create by 2018/5/9 15:09.
 */
import android.app.Application;
import android.content.Context;

import com.bumptech.glide.Glide;
import com.cyh.myapplication.weex.extend.compontent.RichText;
import com.cyh.myapplication.weex.extend.module.PhoneInfoModule;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.taobao.weex.InitConfig;
import com.taobao.weex.WXSDKEngine;
import com.taobao.weex.common.WXException;

import java.io.File;

/**
 * 注意要在Manifest中设置android:name=".WXApplication"
 * 要实现ImageAdapter 否则图片不能下载
 * gradle 中一定要添加一些依赖，否则初始化会失败。
 * compile 'com.android.support:recyclerview-v7:23.1.1'
 * compile 'com.android.support:support-v4:23.1.1'
 * compile 'com.android.support:appcompat-v7:23.1.1'
 * compile 'com.alibaba:fastjson:1.1.45'
 */
public class WXApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
       // initImageLoader(this);
        InitConfig config = new InitConfig.Builder().setImgAdapter(new ImageAdapter(getApplicationContext())).build();
        WXSDKEngine.initialize(this, config);
        try {
            WXSDKEngine.registerModule("poneInfo", PhoneInfoModule.class);
            WXSDKEngine.registerComponent("rich", RichText.class, false);
        } catch (WXException e) {
            e.printStackTrace();
        }
    }


    //使用imageLoade图片请求框架的时候需要调用下面的方法
    public void initImageLoader(Context context){
        //缓存文件的目录
        File cacheDir= StorageUtils.getOwnCacheDirectory(context,"imageloader/cache");
        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(480,480)
                .threadPoolSize(5)
                .threadPriority(Thread.NORM_PRIORITY-2)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URL名称采用MD5加密
               .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(2*1024*1024))
                .memoryCacheSize(2*1024*1024)
                .diskCacheSize(50*1024*1024)
                .diskCache(new UnlimitedDiskCache(cacheDir))
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .imageDownloader(new BaseImageDownloader(context,5*1000,30*1000))
                .writeDebugLogs().build();
        //全局初始化此配置
        ImageLoader.getInstance().init(configuration);
    }
}























